/**
 * @file       godemool/ieee754.cc
 * @brief      Portable ieee754 floating point.
 * @author     ben "boin" bonoeil
 * @date       2015-02
 */

/* Copyright (c) 2007-2015, ben "boin" bonoeil

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   * Neither the name of the <organization> nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
   GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY
   ben "boin" bonoeil ''AS IS'' AND ANY EXPRESS OR IMPLIED
   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL ben "boin" bonoeil BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "ieee754/ieee754.h"

#ifndef NO_IEEE754

typedef union flint {
  uint32_t i;
  float    f;
} flint_u;

float ieee754_i2f(const int32_t i) {
  volatile flint_u u;
  u.i = i;
  return u.f;
}

int32_t ieee754_f2i(const float f) {
  volatile flint_u u;
  u.f = f;
  return u.i;
}

int ieee754_emulated(void) {
  return 0;
}

#else

#include <math.h>
#include <stdlib.h>
#include <assert.h>

#define M_NUL -0.0f
#define P_NUL +0.0f
#define M_NAN -NAN
#define P_NAN +NAN
#define M_INF -INFINITY
#define P_INF +INFINITY

int ieee754_emulated(void) {
  return 1;
}

float ieee754_i2f(const int32_t bits)
{
  float res;

  const int imant = 0x007FFFFF & (bits >> 0);
  const int iexpo = 0x000000FF & (bits >> 23);

  if (!iexpo) {
    if (!imant) {               /* zero */
      res = (bits & (1<<31)) ? M_NUL : P_NUL;
    } else {                    /* Subnormal/Denormalized */
      const float mant = (float)imant;
      float abs = mant * exp2f(-126-23);
      res = (bits & (1<<31)) ? -abs : abs;
    }
  } else if (iexpo == 0xFF) {
    if (!imant) {               /* Infinite */
      res = (bits & (1<<31)) ? M_INF : P_INF;
    } else {                    /* NaN */
      res = (bits & (1<<31)) ? M_NAN : P_NAN;
    }
  } else {                      /* Normalized */
    const float mant = (float)(imant | 0x800000);
    float abs = (float)(mant * exp2f(iexpo-127-23));
    res = (bits & (1<<31)) ? -abs : abs;
  }
  return res;
}

int32_t ieee754_f2i(const float f) {
  int32_t i = (!!signbit(f)) << 31;

  if (isnan(f))
    i |= 0x7fc00000;
  else if (isinf(f))
    i |= 0x7f800000;
  else if (isnormal(f)) {
    /* XXX $$$ TODO */
    i |= 0;
    assert( !"not implemented yet");
  } else {
    /* XXX $$$ TODO */
    i |= 0;
    assert( !"not implemented yet");
  }
  return i;
}

#endif
